<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CIHeadStart Form Helpers
 *
 * @author Casey McLaughlin
 * @package CIHeadStart
 * @link http://code.google.com/p/ciheadstart
 */

 // --------------------------------------------------------------------

/*
 * Form Constants
 */
define('IGNORE_NONCE', FALSE);
define('ANY_FORM', NULL);

 // --------------------------------------------------------------------

/**
 * Checks to see if a form was submitted and performs a CSRF check
 *
 * This only works with forms that were opened using the open_form()
 * function, also in this helper.
 *
 * @param string $submitted_form_name Optional
 * @param boolean $use_nonce Optional
 * @return boolean
 */
function form_was_submitted($submitted_form_name = ANY_FORM, $use_nonce = TRUE)
{
	$ci =& get_instance();

	//If we are getting a specific form by name
	if ($submitted_form_name != ANY_FORM && $ci->input->post('form_submitted_name') != $submitted_form_name)
		return FALSE;

	//If the form_nonce exists in the session and the form_submitted is on
	if ($ci->session->userdata('form_nonce') && $ci->input->post('form_submitted'))
	{
		$nonces = $ci->session->userdata('form_nonce');

		if (strpos($nonces, "|") !== FALSE)
			$nonces = explode("|", $nonces);
		else
			$nonces = array($nonces);

		if (in_array($ci->input->post('form_submitted'), $nonces))
		{
			//Clear the form nonces in the session
			$ci->session->set_userdata('form_nonce', '');
			
			return TRUE;
		}
		elseif ( ! $use_nonce)
			return TRUE;
		else
			return FALSE;
	}
	else
		return FALSE;
}

// --------------------------------------------------------------------

/**
 * Returns an opening form tag in HTML, and performs some
 * hidden CSRF-resistant magic.
 *
 * Should be used in coordination with form_was_submitted()
 *
 * @see form_was_submitted()
 * @param string $form_submit_name Optionally give the form a unique name
 * @param string $action The URL to load (NULL = the current url)
 * @param array|null $attributes Additional form tag attributes (NULL = none)
 * @param array|null $hidden Additional hidden inputs to add, in proper XHTML syntax (NULL = none)
 * @return string
 */
function open_form($form_submit_name=NULL, $action=NULL, $attributes=NULL, $hidden=NULL)
{
	//Create a nonce
	$nonce = random_string('numeric', 32);
	
	//Load the nonce into the session
	$ci =& get_instance();	
	$ci->load->helper('url');

	//If there already nonces, then just add this one to the end using a |
	if (is_string($ci->session->userdata('form_nonce')) && strlen($ci->session->userdata('form_nonce')) > 0)
		$session_nonce = $ci->session->userdata('form_nonce') . "|" . $nonce;
	else
		$session_nonce = $nonce;

	$ci->session->set_userdata('form_nonce', $session_nonce);
	
	//Determine the action
	if ($action == NULL)
		$action = current_url();
	
	//Load the form using the form_open function
	$hidden_items = array('form_submitted' => $nonce);

	if (is_string($form_submit_name) && $form_submit_name != NULL)
		$hidden_items['form_submitted_name'] = $form_submit_name;

	if ($hidden !== NULL && is_array($hidden))
		$hidden_items = array_merge($hidden_items, $hidden);
	
	return form_open($action, $attributes) . "\n<p style='display: none;'>" . form_hidden($hidden_items) . "</p>";
}

// --------------------------------------------------------------------

/**
 * Prints a radio dialog consisting of "on" or "off"
 *
 * @param string $item_name
 * @param int $which 1 or 0; the selected value
 * @param array $yesno_text Consists of two values: array('no_text', 'yes_text')
 * @param string $before HTML to insert before each radio item
 * @param string $after HTML to insert after each radio item
 * @return string
 */
function form_yesno($item_name, $which = 1, $yesno_text = array('no', 'yes'), $before = NULL, $after = NULL)
{
	if ($before == NULL)
		$before = '';
	if ($after == NULL)
		$after = '';

	if ( ! is_array($yesno_text))
		$yesno_text = array('no', 'yes');
	elseif ( ! isset($yesno_text[0]))
		$yesno_text[0] = 'no';
	if ( ! isset($yesno_text[1]))
		$yesno_text[1] = 'yes';

	if ($which != 0 && $which != 1)
		$which = 1;

	if ($which == 0)
		$selected_0 = "checked='checked'";
	else
		$selected_0 = '';

	if ($which == 1)
		$selected_1 = "checked='checked'";
	else
		$selected_1 = '';

	$out_html = $before . "<input type='radio' id='$item_name' name='$item_name' value='1' $selected_1 /> {$yesno_text[1]}" . $after;
	$out_html .= $before . "<input type='radio' id='$item_name' name='$item_name' value='0' $selected_0 /> {$yesno_text[0]}" . $after;

	return $out_html;
}

/* End of file MY_form_helper.php */
/* Location: ./app/helpers/MY_form_helper.php */